	 /* attach a submit handler to the form */
  $("#postForm").submit(function(event) {

    /* stop form from submitting normally */
    event.preventDefault(); 
        
    /* get some values from elements on the page: */
    var $form = $( this ),
        title = $form.find( 'input[name="article_title"]' ).val(),
		url = $form.attr( 'action' );
		text = document.getElementById('ip_text').value;
		cat = document.getElementById('cat').value;
	/* Send the data using post and put the results in a div */
    $.post( url, { text: text, title: title, id: id, cat: cat },
      function( data ) {
          //var content = $( data ).find( '#content' );
          $( "#result" ).empty().append( data );
      }
    );
  });
	//get id 
	var prmstr = window.location.search.substr(1);
	var prmarr = prmstr.split ("&");
	var params = {};
	for ( var i = 0; i < prmarr.length; i++) {
		var tmparr = prmarr[i].split("=");
		params[tmparr[0]] = tmparr[1];
	}
	
	if ( !params.id || params.id <= 0){
		//new post
		id = 0;
	} else {
		id = params.id;
		$(function() {
                $.get('getDetails.php', {id: id}, 
                        function(data) {
								$headText = $(data).filter("#headText").text();	
								$postText = $(data).filter("#postText").text();	
								$( "#article_title" ).val( $headText );
								$( "#ip_text" ).val( $postText );
								$('#htm').html(preview($('#ip_text').val()));
                        }
                );
		});
	}


function preview(text){
	text = "#" + (document.getElementById('article_title').value) + "\n" + text;
	document.getElementById("htm").innerHTML = showd(text);
	insertCaptions();
}


function showd(text){
	var converter = new Showdown.converter();
	var html = converter.makeHtml(text);
	return html;
}

function img(){
$imgurl = prompt("Please enter the url of the image:", "");
if($imgurl){
	$imgname = prompt("Please enter the name/description of the image:", "");
	if($imgname){
		$mdsyntax = "\n![" + $imgname + "](" + $imgurl + ")\n";
		insertAtCursor(document.getElementById("ip_text"), $mdsyntax);
		$('#htm').html(preview($('#ip_text').val()));
	}
}
}

function insertAtCursor(myField, myValue) {
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA/NETSCAPE support
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myField.value.substring(endPos, myField.value.length);
} else {
myField.value += myValue;
}
}

$(document).ready(
	function(){
		$('#save').click(function(){});
	}
);